// 云函数入口文件
const cloud = require('wx-server-sdk')
const moment = require("moment");

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let { remark, point, nickName, avatarUrl,openId} = event
  var success = true
  var message = "添加贡献分成功"
  const db = cloud.database()
  moment.locale('zh-cn');
  date = moment().add(8, 'hours').format('lll');
  systemTime = moment().valueOf();
  var card = {
    _openid: openId,
    type: {
      index: 6,
      name: '贡献分'
    },
    distance: 0,
    time: 0,
    speed: 0,
    message: remark,
    level: 0,
    ropeNo: 0,
    heartrate: 0,
    calorie: 0,
    images: [],
    status: 'unChecked',
    date: date,
    systemTime: systemTime,
    point: parseInt(point) ,
    altitude: 0,
    userInfo: {
      nickName: nickName,
      avatarUrl: avatarUrl,
    }
  }
  await db.collection('cards').add({
    data: card,
    success: function (res) {
      success = true
      message = '添加贡献分成功'
    }
  })

  return {
    success: success,
    message: message,
  }
}